package csce315.group10.androidapp;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import csce315.group10.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class EventEntry extends Activity{
	/* private instance variables */
	CustomApp appState;
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.evententry);
        appState = ((CustomApp)getApplicationContext());
        /* set all fields to Event data */
        Event current_event = getIntent().getParcelableExtra("event");
        
        if(current_event.hasLocation()){
	        /* convert location to valid url string */
	        String temp_location = current_event.get_location();
	        String map_url = "http://maps.googleapis.com/maps/api/staticmap?center=";
	        temp_location = temp_location.replace(' ', '+');
	        map_url += temp_location;
	//        map_url += "&zoom=16&size=400x400&scale=2&sensor=false";
	        map_url += "&size=400x400&markers=color:blue%7Clabel:"+temp_location;
	        map_url += "%7C" +temp_location;
	        map_url+="&scale=2&sensor=false";
	        ImageView location_pic = (ImageView) findViewById(R.id.Event_location_pic);
	        location_pic.setImageDrawable(LoadImageFromWeb(map_url));
	        Log.d("Google map","aquiring image at URL "+map_url);
        }
        
        TextView event_name = (TextView) findViewById(R.id.Event_name_text);
        event_name.setText(current_event.get_event_name());
        TextView event_date = (TextView) findViewById(R.id.Event_date_text);
        event_date.setText(current_event.get_event_date());
        TextView event_time = (TextView) findViewById(R.id.Event_time_text);
        event_time.setText(current_event.get_event_time());
        TextView contact_name = (TextView) findViewById(R.id.Contact_name_text);
        contact_name.setText(current_event.get_con_name());
        TextView contact_phone = (TextView) findViewById(R.id.Contact_phone_text);
        contact_phone.setText(current_event.get_con_phone());
        TextView staffer_covering = (TextView) findViewById(R.id.Staffer_covering_text);
        staffer_covering.setText(current_event.get_staffer());
        TextView location = (TextView) findViewById(R.id.Location_text);
        location.setText(current_event.get_location());
        TextView notes = (TextView) findViewById(R.id.Notes_text);
        notes.setText(current_event.get_notes());

    }
    
    public static Drawable LoadImageFromWeb(String url) {
        try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "location_image");
            return d;
        } catch (Exception e) {
            return null;
        }
    }
   
}